<template>
	<view class="login">
		<view class="txt_box">
			<view class="txt">大树宠医，需要您的授权同意</view>
			<view class="txt">才能获取您的信息用于显示（昵称、头像等）</view>
		</view>
		<button class="btn_login" @click="getUserProfile">微信授权登录</button>
	</view>
</template>

<script>
let app = getApp();
// import {
// 	login
// } from '@/api/index.js'
export default {
	data() {
		return {
			date: ''
		};
	},
	methods: {
		getUserProfile() {
			wx.getUserProfile({
				desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				success: res => {
					console.log(res);
					// errMsg: "getUserProfile:ok"
					if (res.errMsg === 'getUserProfile:ok') {
						let userInfo = res.userInfo;
						uni.login({
							provider: 'weixin',
							success: loginRes => {
								// 获取openid
								login({
									jscode: loginRes.code,
									nickname: userInfo.nickName,
									language: userInfo.language,
									country: userInfo.country,
									pro: userInfo.province,
									city: userInfo.city,
									headimgurl: userInfo.avatarUrl,
									sex: userInfo.gender == 0 ? '未知' : userInfo.gender == 1 ? '男' : '女' //0：未知、1：男、2：女
								}).then(res => {
									console.log(userInfo, 98989898);
									let openid = res.openid;
									app.globalData.openid = openid;
									uni.setStorageSync('openid', openid); //本地存储
									uni.setStorageSync('userInfo', { nickname: userInfo.nickName, headimgurl: userInfo.avatarUrl }); //本地存储
									uni.navigateBack();
								});
							}
						});
					}
				}
			});
		}
	},
	onLoad() {
		console.log(app.globalData.openid);
	}
};
</script>

<style lang="scss">
.login {
	padding: 32rpx;
	.txt_box {
		margin-top: 240rpx;
		color: #666666;
		text-align: center;

		.txt {
			margin-bottom: 14rpx;
		}
	}
	.btn_login {
		width: 100%;
		padding: 0;
		height: 80rpx;
		line-height: 80rpx;
		background: #34d1a9;
		border-radius: 40rpx;
		color: #ffffff;
		font-size: 36rpx;
		margin-top: 152rpx;
	}
}
</style>
